package com.sinosoft.cloud.access.handler;

import com.sinosoft.cloud.access.net.MessageObject;
import com.sinosoft.cloud.access.persist.Persister;
import io.netty.channel.ChannelHandlerContext;

/**
 * cloud-netty
 *
 * @title: cloud-netty
 * @package: com.sinosoft.cloud.access.handler
 * @author: yangming
 * @date: 2017/12/3 下午12:27
 */
public class MessagePersistentHandler extends AbstractHandler {

    private String messageType;

    @Override
    MessageObject dealMessage(ChannelHandlerContext ctx, MessageObject messageObject) {

        if (accessControl.getBarrierControl()){
            return messageObject;
        }

        Persister persister = getPersister();
        String persistMessage = "";

        if ("In".equalsIgnoreCase(this.getMessageType())){
            persistMessage = messageObject.getDecryptMsg();
        }

        if ("Out".equalsIgnoreCase(this.getMessageType())){
            persistMessage = messageObject.getSendToBankMsg();
        }

        persister.msgPersistent(persistMessage, messageObject.getTranData(),this.getMessageType());
        messageObject.setNext(MessageObject.Type.fireChannelRead);
        return messageObject;
    }

    public String getMessageType() {
        return messageType;
    }

    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }
}
